
//
//  RBShopListCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/18.
//

#import "RBShopListCell.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import <UIImage+YYAdd.h>
#import "UIScrollView+RBAddHorizontal.h"
#import "RBShopDetailVC.h"
#import "RBShopResponse.h"

@interface RBShopListCell()

@property(nonatomic, strong)UIView *containerView;
@property(nonatomic, strong)UIView *topView;
@property(nonatomic, strong)UIImageView *shopImgView;
@property(nonatomic, strong)UILabel *shopNameL;
@property(nonatomic, strong)UILabel *shopDesL;
@property(nonatomic, strong)UIScrollView *bottomView;

@property(nonatomic, strong) RBCellModel *cellModel;

@end

@implementation RBShopListCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
    }
    return self;
}

#pragma mark - Event

- (void)enterShopBtnTapped
{
    RBShopModel *model = self.cellModel.cellData;
    RBShopDetailVC *vc = RBShopDetailVC.new;
    vc.id = model.id;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

- (void)setCellModel:(RBCellModel *)cellModel
{
    _cellModel = cellModel;
    [self.containerView view_clear];
    _topView = nil;
    _bottomView = nil;
    [self.containerView view_addView:self.topView withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    RBShopModel *model = cellModel.cellData;
//    if(model.goods.count)
//    {
        [self.containerView view_addView:self.bottomView withHeight:Zoom(110) width:CGFLOAT_MIN top:0 bottom:Zoom(10) left:Zoom(10) right:Zoom(10) centerX:0];
//    }
}

#pragma mark - Getter

- (UIView *)containerView
{
    if (!_containerView)
    {
        self.containerView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [view rbSetCornerRadius:Zoom(8)];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
                make.width.mas_equalTo(Zoom(365));
            }];
            view;
        });
    }
    return _containerView;
}

- (UIView *)topView
{
    if (!_topView)
    {
        self.topView = ({
            RBShopModel *model = self.cellModel.cellData;
            UIView *view = UIView.new;
            [view h_view_addView:({
                UIImageView *view = [RanTool imgViewWithMode:2 radius:Zoom(17) imgName:model.logo];
                self.shopImgView = view;
                view;
            }) withHeight:Zoom(34) width:Zoom(34) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(10) right:0 centerY:YES];
            [view h_view_addView:({
                UIView *view = UIView.new;
                [view view_addView:({
                    UILabel *view = [RanTool labelWithFontSize:Zoom(14) weight:1 color:@"000000" align:0 text:model.store_name];
                    self.shopNameL = view;
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
                [view view_addView:({
                    UILabel *view = UILabel.new;
                    UIFont *font = [UIFont systemFontOfSize:Zoom(12)];
                    UIColor *color = [UIColor colorWithHexString:@"#A6A5A3"];
                    view.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
                        CGFloat point = model.pingfen;
                        for (NSInteger i = 1; i <= 5; i++)
                        {
                            if (i != 1)
                            {
                                [maker addAttributeStringWithImage:[UIImage imageWithColor:UIColor.clearColor] imageSize:CGSizeMake(Zoom(1), Zoom(1)) maxFont:font];
                            }
                            NSString *imgName = @"";
                            if (point >= i)
                            {
                                imgName = @"shopStartSel";
                            }
                            else if(point > i - 1)
                            {
                                imgName = @"shopStartHalf";
                            }
                            else
                            {
                                imgName = @"shopStart";
                            }
                            [maker addAttributeStringWithImage:[UIImage imageNamed:imgName] imageSize:CGSizeMake(Zoom(9), Zoom(8)) maxFont:font];
                        }
                        [maker addAttributeStringWithFont:font color:color string:[NSString stringWithFormat:@" %.1f|收藏量%@|评论%@|销量%@",model.pingfen,[self numDesFromInt:model.shoucang],[self numDesFromInt:model.pinglun],[self numDesFromInt:model.xiaoliang]]];
                    }];
                    self.shopDesL = view;
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(8) bottom:0 left:0 right:0 centerX:0];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(15) bottom:Zoom(15) left:Zoom(14) right:0 centerY:NO];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:1 color:@"#518CFF" align:1 text:@"进店" bgColor:UIColor.whiteColor radius:Zoom(11)];
                [view rbSetBorderWitdh:1 borderColor:[UIColor colorWithHexString:@"#518CFF"]];
                [view rbAddTapGesWithTarget:self action:@selector(enterShopBtnTapped)];
                [view setResponseArea:UIEdgeInsetsMake(Zoom(12), Zoom(12), Zoom(12), Zoom(12))];
                view;
            }) withHeight:Zoom(22) width:Zoom(44) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(2) right:Zoom(10) centerY:YES];
            view;
        });
    }
    return _topView;
}

- (NSString *)numDesFromInt: (NSInteger)intNum
{
    if(intNum < 1000)
    {
        return [NSString stringWithFormat:@"%zd", intNum];
    }
    else
    {
        return [NSString stringWithFormat:@"%.1f", intNum/10000.0];
    }
}

- (UIScrollView *)bottomView
{
    if (!_bottomView)
    {
        self.bottomView = ({
            RBShopModel *model = self.cellModel.cellData;
            UIScrollView *view = UIScrollView.new;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            [view h_rbSetContentHeight:Zoom(110)];
            for (NSInteger i = 0; i < model.goods.count; i++)
            {
                RBShopGoodsModel *goodsModel = model.goods[i];
                [view h_view_addView:[RanTool imgViewWithMode:2 radius:0 imgName:goodsModel.picurl] withHeight:Zoom(110) width:Zoom(110) top:0 bottom:0 left:i? Zoom(10): 0 right:0 centerY:0];
            }
            view;
        });
    }
    return _bottomView;
}

@end
